<template>
  <div class="cate-wrap" ref="categoryWrap">
    <section class="cate-nav" ref="sidewrapper">
      <ul class="cate-nav-list">
        <li v-for="(item, index) in cateList" :key="index" @click="selectMenu(index)" :class="{ active: currentIndex === index }">{{ item.name }}</li>
      </ul>
    </section>
    <section class="cate-con" ref="contentwrapper">
      <div v-for="(item, index) in categoryList" :key="index">
        <img :src="item.mainImgUrl" alt="" class="main-prod" />
        <div class="cate-con-item" v-for="(category, index) in item.list" :key="index">
          <p class="item-title">{{ category.title }}</p>
          <div class="item-prod" v-for="(prod, index) in category.productList" :key="index">
            <img :src="prod.imgUrl" alt="" class="prod-img" />
            <p class="prod-title">{{ prod.title }}</p>
          </div>
        </div>
      </div>
    </section>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
//#region
/* 记得加筛选功能 <section class="select-menu">
        <div class="select-item">默认排序</div>
        <div class="select-item">
            升序
            <i class="iconfont icon-up1"></i>
        </div>
        <div class="select-item">
            降序
            <i class="iconfont icon-down1"></i>
        </div>
        <div class="select-item">
            筛选
            <i class="iconfont icon-shaixuan"></i>
        </div>
*/
//#endregion
import { getCategory } from '@/http/index.js'
export default {
  data() {
    return {
      cateList: [],
      currentIndex: 0,
    }
  },
  created() {
    this.fetchCateList()
    this.$forceUpdate()
  },
  computed: {
    categoryList() {
      return this.cateList.filter((val, index) => this.currentIndex == index)
    },
  },
  mounted() {
    this.setSearchWrapHeight()
  },
  methods: {
    setSearchWrapHeight() {
      let $screenHeight = document.documentElement.clientHeight
      this.$refs.categoryWrap.style.height = $screenHeight - 100 + 'px'
    },
    fetchCateList() {
      // this.$http.get('/category/data').then(async (res) => {
      //   const { data } = await res
      //   if (data.status == 0) {
      //     this.cateList = data.data
      //   }
      // })
      getCategory().then((res) => {
        this.cateList = res.data
        this.$nextTick(() => {
          new BScroll(this.$refs.sidewrapper, {
            probeType: 3,
            pullUpLoad: true,
            click: true,
            bounce: {
              top: true,
              bottom: true,
              left: true,
              right: true,
            },
          })
          new BScroll(this.$refs.contentwrapper, {
            probeType: 3,
            pullUpLoad: true,
            click: true,
            bounce: {
              top: true,
              bottom: true,
              left: true,
              right: true,
            },
          })
        })
      })
    },
    selectMenu(index) {
      this.currentIndex = index
    },
  },
}
</script>

<style lang="scss" scoped>
@import '@/assets/css/mixin.scss';
.cate-wrap {
  @include fj;
  margin-top: 100px;
  width: 100%;
  background-color: white;
  overflow: hidden;
  .cate-nav {
    width: 30%;
    overflow: hidden;
    box-sizing: border-box;
    .cate-nav-list {
      width: 100%;
      box-sizing: border-box;
      background-color: #f8f8f8;
      li {
        width: 100%;
        height: 92px;
        text-align: center;
        line-height: 92px;
        font-size: 28px;
        &.active {
          color: $red;
          background: #fff;
        }
      }
    }
  }
  .cate-con {
    padding: 0 20px;
    width: 70%;
    overflow: hidden;
    background-color: white;
    box-sizing: border-box;
    .main-prod {
      width: 100%;
    }
    .cate-con-item {
      display: flex;
      flex-wrap: wrap;
      flex-shrink: 0;
      width: 100%;
      .item-title {
        padding: 20px 0;
        width: 100%;
        font-size: 36px;
      }
      .item-prod {
        margin-bottom: 10px;
        width: 33.33%;
        text-align: center;
        font-size: 30px;
        .prod-img {
          width: 100%;
        }
      }
    }
    .cate-con-item:last-of-type {
      margin-bottom: 100px;
    }
  }
}
</style>
